<?php
	require 'login.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <?php
            include('generico.php');
    
	generaHead();
	?>
    <body>
        <div id="container">
        <?php
		
            generaHeader();
            generaMenusx(isset($_GET['t']));
            generaMenuLogin();
            echo '<div id="content">';
			if(isset($_GET['t'])){
				$t = cleanNumber($_GET['t']);
				$qth = "SELECT Titolo, NumeroPost, Categoria, Stato FROM Threads WHERE Id = '".$t."';";
				$thread = mysql_query($qth);
				$thread = mysql_fetch_assoc($thread);

				$t = cleanNumber($_GET['t']);
				if(isset($_GET['pag']))
					$pag = cleanNumber($_GET['pag']);
				else 
					$pag = ((int)(($thread['NumeroPost']-1)/10));
				
				$qry="SELECT Autore, Corpo, Id,  Data From Posts WHERE Thread = '".$t."'
						ORDER BY Data ASC LIMIT ".($pag*10).", 10 ";
				
				$posts=mysql_query($qry);

			}
			if(isset($posts)) {
				echo '<h3>'.$thread['Titolo'];
				if($thread['Stato']=='Chiuso')
					echo ' (Chiuso)';

				echo '</h3>
					<a href="categoria.php?cat='.$thread['Categoria'].'">Torna a '.$thread['Categoria'].'</a>';
				echo '<div id="posts">';
				generaPosts($posts);
				echo '</div>';//posts

				//Frecce di navigazione pagine
				echo '<p class="navPagine">';
				if($pag > 0)
					echo '<a href="thread.php?t='.$t.'&amp;pag='.($pag-1).'" title="Pagina precedente">&lt;&lt; indietro</a>';
				else
					echo '<span class="gray">&lt;&lt; indietro</span>';
				echo '&emsp;';
				if($pag!=0) 
					echo '<a href="thread.php?t='.$t.'&amp;pag=0" title="Prima pagina">1</a>';
				else
					echo '<span class="gray" title="Pagina successiva">1</span>';

				echo ' . . . ';
				echo '<span title="Pagina corrente">'.($pag+1).'</span>';
				echo ' . . . ';

				if((($pag+1)*10) < $thread['NumeroPost']) {
					echo '<a href="thread.php?t='.$t.'&amp;pag='.((int)($thread['NumeroPost']/10)).'" title="Ultima pagina">'.((int)($thread['NumeroPost']/10)+1).'</a>';					
					echo '&emsp;';
					echo '<a href="thread.php?t='.$t.'&amp;pag='.($pag+1).'" title="Pagina successiva">avanti &gt;&gt;</a>';
				}
				else {
					echo '<span class="gray" title="Ultima pagina">'.((int)($thread['NumeroPost']/10)+1).'</span>';
					echo '&emsp;';	
					echo '<span class="gray" title="Pagina successiva">avanti &gt;&gt;</span>';
				}
				


				echo '</p>';
			}
			echo '</div>';//content
				
            
            generaFooter();
        ?>
        </div>
    </body>
</html>

